package com.zhq.blog.core.domain.money;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.springframework.data.jpa.domain.Specification;

import com.zhq.blog.core.domain.BasePersistentDomain;
import com.zhq.blog.core.domain.search.CardRecordSearch;

@Entity
public class CardRecord extends BasePersistentDomain<CardRecordRepository> {
	
	private String order_no;  //订单号
	private String userName; //用户名字
	private long price;		//价格
	private String cardType;  //充值卡类型
	private long nums;	//数量
	
	
	public CardRecord(){}
	
	public CardRecord(String order_no, String userName, long price, String cardType,
			long nums) {
		super();
		this.order_no = order_no;
		this.userName = userName;
		this.price = price;
		this.cardType = cardType;
		this.nums = nums;
	}
	
	

	public String getOrder_no() {
		return order_no;
	}

	public void setOrder_no(String order_no) {
		this.order_no = order_no;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public long getPrice() {
		return price;
	}

	public void setPrice(long price) {
		this.price = price;
	}

	public String getCardType() {
		return cardType;
	}

	public void setCardType(String cardType) {
		this.cardType = cardType;
	}

	public long getNums() {
		return nums;
	}

	public void setNums(long nums) {
		this.nums = nums;
	}

	public  Specification<CardRecord> selectCondition(final CardRecordSearch cardRecordSearch) {
		return new Specification<CardRecord>() {
			@Override
			public Predicate toPredicate(Root<CardRecord> root,
					CriteriaQuery<?> query, CriteriaBuilder builder) {

				Predicate predicate = builder.conjunction();
				List<Expression<Boolean>> expressions = predicate.getExpressions();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

				if (StringUtils.isNotBlank(cardRecordSearch.getOrder_no())) {
					expressions.add(builder.or( builder.like(root.<String> get("order_no"), "%"+ cardRecordSearch.getOrder_no() + "%")));

					
				}
				
				

				if(StringUtils.isNotBlank(cardRecordSearch.getCardRecordStartDate()) && StringUtils.isBlank(cardRecordSearch.getCardRecordEndDate())){
					Date star = null;
					try {
						star = sdf.parse(cardRecordSearch.getCardRecordStartDate());
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					expressions.add(builder.greaterThanOrEqualTo(root.get("createdDate"), star)); 
				}
				
				if(StringUtils.isBlank(cardRecordSearch.getCardRecordStartDate())&& StringUtils.isNotBlank(cardRecordSearch.getCardRecordEndDate())){
					Date end = null;
					try {
						end = sdf.parse(cardRecordSearch.getCardRecordEndDate());
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					expressions.add(builder.lessThanOrEqualTo(root.get("createdDate"), end)); 
				}

				if(StringUtils.isNotBlank(cardRecordSearch.getCardRecordStartDate()) && StringUtils.isNotBlank(cardRecordSearch.getCardRecordEndDate())){
					
					Date star = null;
					Date end = null;
					try {
						star = sdf.parse(cardRecordSearch.getCardRecordStartDate());
						end = sdf.parse(cardRecordSearch.getCardRecordEndDate());
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					expressions.add(builder.between(root.get("createdDate"), star, end)); // 创建时间createdTime
				}

				return query.where(predicate).getRestriction();

			}
		};
	}
	
	
	
}
